package com.ldgk.pim.client.view.gwt;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.ldgk.pim.client.presenter.PlacePresenter;
import com.ldgk.pim.client.presenter.Presenter;
import com.ldgk.pim.client.view.PlaceView;
import com.ldgk.pim.client.view.components.DistributionPage;
import com.ldgk.pim.data.Place;

//TODO acertar action buttons, tamanhos, etc etc.
public class PlaceViewGXTImpl implements PlaceView{
	private PlacePresenter presenter;
	private FormPanel form;
	private FormBinding binding;
	private final BeanModelFactory beanModelFactory = BeanModelLookup.get().getFactory(Place.class);
	private final Window window = new Window();  

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = (PlacePresenter)presenter;
	}

	@Override
	public void editPlace(Place distributionPlace) {
		BeanModel placeModel = beanModelFactory.createModel(distributionPlace != null? distributionPlace: new Place());
		placeModel.set("phones", ((String)placeModel.get("phones")).replace('|','\n'));
		binding.bind(placeModel);
	    window.show();		
	}
	
	public PlaceViewGXTImpl(){
		this.buildForm();

		window.setSize(450, 580);  
	    window.setPlain(true);  
	    window.setModal(true);  
	    window.setBlinkModal(true);  
	    window.setHeading("Local de Distribuicao");  
	    window.setLayout(new FitLayout());  
	    window.add(form, new FitData(1));  
	}

	
	private void buildForm(){
		form = new FormPanel();
		form.setHeaderVisible(false);

		binding = new FormBinding(form);

		//Name
	    TextField<String> name = new TextField<String>();
	    name.setName("name");
	    name.setFieldLabel("Nome");
	    name.setAllowBlank(false);
	    form.add(name);
	  
	    //Address
	    TextField<String> address = new TextField<String>();
	    address.setName("address");
	    address.setFieldLabel("Endere&ccedil;o");  
	    form.add(address);
	    
	    
	    //Phones
	    TextArea phones = new TextArea();  
	    phones.setName("phones");
	    phones.setFieldLabel("Telefones");
	    form.add(phones);
	  
	    //E-mail
	    TextField<String> email = new TextField<String>();
	    email.setName("email");
	    email.setFieldLabel("E-mail");
	    form.add(email);

	    //Contact
	    TextField<String> contact = new TextField<String>();
	    contact.setName("contactPerson");
	    contact.setFieldLabel("Contato");
	    form.add(contact);

	    //Details
	    TextArea details = new TextArea();  
	    details.setPreventScrollbars(true);
	    details.setName("details");
	    details.setFieldLabel("Observa&ccedil;&otilde;es");
	    form.add(details);  

	    //Types
	    SimpleComboBox<BaseModel> typesCB = new SimpleComboBox<BaseModel>();
	    typesCB.setFieldLabel("Tipo");
	    
	    typesCB.setId("id");
	    typesCB.setDisplayField("name");
	    typesCB.setName("type");
	    
	    BaseModel model = new BaseModel();
	    model.set("id",1);
	    model.set("name","Escola");
	    typesCB.add(model);

	    model = new BaseModel();
	    model.set("id",2);
	    model.set("name","Igreja");
	    typesCB.add(model);

	    model = new BaseModel();
	    model.set("id",3);
	    model.set("name","Hospital");
	    typesCB.add(model);

	    model = new BaseModel();
	    model.set("id",4);
	    model.set("name","Outros");
	    typesCB.add(model);

	    form.add(typesCB);
	  
	    //Distribution places
	    FieldSet distributionsSet = new FieldSet();
	    distributionsSet.setHeight(280);
	    distributionsSet.setAutoWidth(true);
	    distributionsSet.setHeading("Distribuicoes Realizadas");  
	    distributionsSet.setCollapsible(true);  
	    distributionsSet.setLayout(new FitLayout()); 
	    distributionsSet.add(new DistributionPage());
	    
	    form.add(distributionsSet);  
	    form.setButtonAlign(HorizontalAlignment.CENTER);
	    
	    Button btnSave = new Button("Salvar",new SelectionListener() {
			public void componentSelected(ComponentEvent ce) {
				BeanModel beanModel = (BeanModel)binding.getModel();
				Place place = (Place)beanModel.getBean();
				presenter.savePlace(place);
			}
		});
	    
	    form.addButton(btnSave);  
	    form.addButton(new Button("Cancelar")); 
	    
	    binding.autoBind();
	}
}
